package com.runen.infinitemovement.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.widget.RemoteViews;

import androidx.annotation.DrawableRes;
import androidx.core.app.NotificationCompat;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.tool.CommonUtil;

import java.util.Locale;

/**
 * Author: SXF
 * E-mail: xue.com.fei@outlook.com
 * CreatedTime: 2018/5/4 10:01
 * <p>
 * SportsNotification
 */
public class SportsNotification {
    private static final int ID = 0x0568;
    private static final int GROUP_ID = 0x0578;
    private Context context;
    private int id;
    private Notification notification;
    private NotificationManager notificationManager;

    public SportsNotification(Context context) {
        this(context, ID);
    }

    public SportsNotification(Context context, int id) {
        this.context = context;
        this.id = id;
        init();
    }

    private void init() {
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_sports);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(String.valueOf(id),
                    CommonUtil.getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(false);
            //channel.setLightColor(Color.GREEN);
            channel.setShowBadge(false);
            channel.setSound(null, null);
            channel.setVibrationPattern(null);
            notificationManager.createNotificationChannel(channel);
        }
        notification = new NotificationCompat.Builder(context, String.valueOf(id))
                .setChannelId(String.valueOf(id))
                .setWhen(System.currentTimeMillis())
                .setGroupSummary(false)
                .setGroup(String.valueOf(GROUP_ID))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setAutoCancel(false)
                .setOngoing(true)
                .setDefaults(Notification.FLAG_AUTO_CANCEL)
                .setSmallIcon(R.mipmap.ico_logo)
                .setCustomBigContentView(remoteViews)
                .setCustomContentView(remoteViews)
                .build();
        notificationManager.notify(id, notification);
    }

    /**
     * 设置意图
     *
     * @param intent
     * @return SimplePedometerNotification
     */
    public SportsNotification setContentIntent(PendingIntent intent) {
        notification.contentIntent = intent;
        return this;
    }

    /**
     * 设置运动图标
     *
     * @param icon
     */
    public SportsNotification setIcon(@DrawableRes int icon) {
        notification.contentView.setImageViewResource(R.id.iv_notification_sports_icon, icon);
        return this;
    }

    /**
     * 设置运动运动距离
     *
     * @param distance
     * @return
     */
    public SportsNotification setDistance(double distance) {
        notification.contentView.setTextViewText(R.id.tv_notification_sports_distance,
                context.getString(R.string.notification_sports_distance, distance));
        return this;
    }

    /**
     * 设置运动卡路里
     *
     * @param calorie
     * @return
     */
    public SportsNotification setCalorie(int calorie) {
        notification.contentView.setTextViewText(R.id.tv_notification_sports_calorie,
                context.getString(R.string.notification_sports_calorie, calorie));
        return this;
    }

    /**
     * 设置运动时间
     *
     * @param millisecond
     */
    public SportsNotification setTime(long millisecond) {
        int minute, second;
        millisecond = millisecond / 1000;
        second = (int) (millisecond % 60);
        minute = (int) (millisecond / 60);
        notification.contentView.setTextViewText(R.id.tv_notification_sports_time,
                context.getString(R.string.notification_sports_time, String.format(Locale.getDefault(), "%02d:%02d", minute, second)));
        return this;
    }

    /**
     * 获取通知栏ID
     *
     * @return
     */
    public int getNotificationId() {
        return id;
    }

    /**
     * 获取通知栏
     *
     * @return Notify
     */
    public Notification getNotification() {
        return notification;
    }

    /**
     * 通知通知栏
     */
    public void notifyChanged() {
        notificationManager.notify(id, getNotification());
    }

    /**
     * 取消通知栏
     */
    public void cancel() {
        notificationManager.cancel(id);
    }

}
